/*
 *	The temperature program prints table for fahrenheit-celsius 
 *	and celsius-fahrenheit.
 *	Copyright (C) 1989-2089 Sergey Sergeevich Tsybanov All Rights Reserved
 *
 *	Temperature is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Temperature is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License 
 *	along with the temperature program. 
 *	If not, see <https://www.gnu.org/licenses/>.
 */

#include "../includes/temperature.h"

int main( void)
{
	printTemperature( true, 300, 0, 14.44);
	printTemperature( false, 25, 345, -23.71);
	return 0;
}

INLINE MATH_DECIMAL absoluteValue( MATH_DECIMAL x) {
	MATH_INTEGER y = *( MATH_INTEGER*) &x;
	y &= CLEAR_MOST_SIGNIFICANT_BIT;
	return *( MATH_DECIMAL*) &y;
}

void printTemperature( bool isCelsius, MATH_DECIMAL start, 
			MATH_DECIMAL end, MATH_DECIMAL step) {
	step = absoluteValue( step);
	if ( start != end || absoluteValue( start - end) > step) {
		if ( isCelsius) {
			printf( "%s\n%s\n%s\n%s\n%s\n",
				"=====================",
				"= Temperature Conv. =",
				"=====================",
				"== Co  =======  Fo ==",
				"=====================");
			if ( start > end)
				for ( ; start >= end; start -= step)
					printf( "%6.0f\t%10.3f\n", start,
						9.0 / 5 * start + 32);
			else {
				for ( ; start <= end; start += step)
					printf( "%6.0f\t%10.3f\n", start,
						5.0 / 9 * ( start - 32));
			}
		}
		else {
			printf( "%s\n%s\n%s\n%s\n%s\n",
				"=====================",
				"= Temperature Conv. =",
				"=====================",
				"== Fo  =======  Co ==",
				"=====================");
			if ( start > end)
				for ( ; start >= end; start -= step)
					printf( "%6.0f\t%10.3f\n", start,
						9.0 / 5 * start + 32);
			else {
				for ( ; start <= end; start += step)
					printf( "%6.0f\t%10.3f\n", start,
						5.0 / 9 * ( start - 32));
			}
		}
	}
}